August 16, 2020

Code for the trees dataset

mod = lm(Volume ~ Girth, data=trees)
x = trees$Girth; y = trees$Volume

xax <- list(
  title = "Girth",
  titlefont = list(family="Modern Computer Roman")
)
yax <- list(
  title = "Volume",
  titlefont = list(family="Modern Computer Roman"),
  range = c(0,80)
)

fig <- plot_ly(x=x, y=y, type="scatter", mode="markers", name="data",
        width=800, height=430) %>%
        add_lines(x = x, y = fitted(mod), name="fitted") %>%
        layout(xaxis = xax, yaxis = yax) %>%
        layout(margin=list(
            l=150,
            r=50,
            b=20,
            t=40
            )
        )
config(fig, displaylogo=FALSE)

mpg VS. wt from dataset mtcars; color=disp

data(mtcars)
attach(mtcars)
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
xax <- list(
  title = "Weight",
  titlefont = list(family="Modern Computer Roman")
)

yax <- list(
  title = "Miles per Gallon",
  titlefont = list(family="Modern Computer Roman")
)

plot_ly(mtcars, x=wt, y=mpg, type="scatter", mode="markers", color=disp) %>%
  layout(xaxis= xax, yaxis=yax)

mpg VS. wt from dataset mtcars

color = # cylinders; size = hp

data(mtcars); attach(mtcars)
plot_ly(mtcars, x=wt, y=mpg, mode="markers", 
        color=as.factor(cyl), size=hp,
        width=600, height=350) %>%
  layout(xaxis= xax, yaxis=yax)

Random Triples

\(X\sim \mathcal{N}(\mu=30; \sigma^2=5^2)\); \(Y\sim \mathcal{N}(0; 1); Z\sim \mathcal{N}(15; 2^2)\); color=\(Z\)

\(X, Y, Z\) - independent

Code for random triples

set.seed(123)
myX = rnorm(100, mean=30, sd=5)
myY = rnorm(100)
myZ = rnorm(100, mean=15, sd=2)
plot_ly(x=myX, y=myY, z=myZ,
        type="scatter3d", mode="markers",
        color=myZ) %>%
  hide_colorbar()

Plot of mpg VS. (wt,hp) from mtcars dataset; color = # cylinders

data(mtcars)

xax <- list(
  title = "wt",
  titlefont = list(family="Modern Computer Roman")
)
yax <- list(
  title = "hp",
  titlefont = list(family="Modern Computer Roman")
)
zax <- list(
  title = "mpg",
  titlefont = list(family="Modern Computer Roman")
)

plot_ly(data=mtcars, x=wt, y=hp, z=mpg,
        type="scatter3d", mode="markers",
        color=as.factor(cyl)) %>%
   layout(
      title = "Miles per Gallon VS. (Weight, Horsepower)",
      scene=list(xaxis=xax, yaxis=yax, zaxis=zax))

Note the smaller font!

data(mtcars)

xax <- list(
  title = "wt",
  titlefont = list(family="Modern Computer Roman")
)
yax <- list(
  title = "hp",
  titlefont = list(family="Modern Computer Roman")
)
zax <- list(
  title = "mpg",
  titlefont = list(family="Modern Computer Roman")
)

plot_ly(data=mtcars, x=wt, y=hp, z=mpg,
        type="scatter3d", mode="markers",
        color=as.factor(cyl)) %>%
   layout(
      title = "Miles per Gallon VS. (Weight, Horsepower)",
      scene=list(xaxis=xax, yaxis=yax, zaxis=zax))

Plots of   \(f(x,y)=\sin(x+1) + y^2\)   and   \(f(x,y) = 0.6\,x + 0.8\)

x = seq(-6,6,by=0.05);  y = seq(-2,2,by=0.05)

## creating meshgrid of (x,y) points
X = matrix(rep(x,length(y)),nrow=length(y), byrow=TRUE)
Y = matrix(rep(y,length(x)),ncol=length(x), byrow=FALSE)

## creating a surface Z=f(X,Y)
Z = sin(X + 1) + Y^2  

## creating a plane
Zp = 0.6*X + 0.8  

fig <- plot_ly(x = X, y = Y, z = Z, showscale=F, width=800, height=550) %>% 
   add_surface(
      contours = list(      #add contours
          z = list(
             show=TRUE,
             usecolormap=TRUE,
             highlightcolor="#ff0000",
             project=list(z=TRUE)
          ) #end of z list
        ) #end of contours list
) #end of add_surface() function

fig <- fig %>% layout(list(
   scene = list(camera=list(eye = list(x=1.87, y=0.88, z=-0.6)))))

## fig <- hide_colorbar(fig)
fig <- fig %>% add_surface(z=Zp)  #add the plane to the plot
fig ##show/plot the figure

Latex Syntax

Greek letters, subscripts, superscripts, fractions, sums, integrals, …

\(y = \beta_0 + \beta_1\cdot x + \varepsilon\), where \(\varepsilon \sim \mathcal{N}(\mu=0; \,\,\sigma^2)\)

\(\displaystyle MSE = {SSE \over n-2} = {1\over n-2} \sum_{i=1}^n (y_i - \hat{y}_i)^2\)

\(\displaystyle \int_0^{\infty}e^{-2x}dx = \left(-{1\over 2}\right) e^{-2x}\Big|_{x=0}^{x\to\infty} = \left(-{1\over 2}\right)(0 - e^0) = {1\over 2}\)

\(\forall a, b \in \mathbf{R}\) with \(a\neq b\), we have: \({a^2 - b^2 \over a-b} = a+b\).

\(\sqrt{\sum_{i=1}^n (x_i+y_i)^2} = \|x + y\| \leq \|x\| + \|y\|\)

ASU (Tempe) and Cities in Phoenix Metropolitan Area

asuicon = makeIcon(iconUrl="asu-logo.png", iconWidth=31*215/80, iconHeight=60, 
                   iconAnchorX = 31*215/230/2, iconAnchorY=16)
anglerIcon <- makeIcon(
   iconUrl = "people.svg",
   iconWidth = 30, iconHeight = 30, iconAnchorX = 22, iconAnchorY = 40,
   shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
   shadowWidth = 20, shadowHeight = 30, shadowAnchorX = -4, shadowAnchorY = 22)
asulinks = c("<a href='https://sols.asu.edu'>Life Sciences Tower - E Wing (LSE) <br> School of Life Sciences </a>",
             "<a href='https://engineering.asu.edu'> Brickyard Engineering (BYENG) <br> Ira Fulton School of Engineering </a>",
             "<a href='https://physics.asu.edu'>Physical Sciences Building - F Wing (PSF) <br> Department of Physics  </a>",
             "<a href='https://math.asu.edu'> Wexler Hall (WXLR) <br> School of Math & Stats Sciences</a>",
             "<a href='https://math.asu.edu'> Engineering Center A-wing (ECA) <br> School of Math & Stats Sciences <br>  (another Math building)</a>",
             "<a href='https://barretthonors.asu.edu'> Honors Hall (HONHALL)  <br> Barrett Honors College</a>")
lat.lng.df = data.frame(lat=c(33.419493,33.423508, 33.420864, 33.420638, 33.418596, 33.415757),
                        lng=c(-111.933794,-111.939567, -111.931525, -111.932002, -111.932178, -111.927700))
cities = data.frame(name = c("Mesa", "Gilbert", "Tempe", "Chandler", "Glendale", "Scottsdale"),
                    population = c(496401, 242354, 185038, 253458, 203054, 249950),
                    lat = c(33.42227, 33.349372, 33.424676, 33.303869, 33.538324, 33.493989),
                    lng = c(-111.82264,  -111.790811, -111.938309, -111.844266, -112.186124, -111.926126))
mymap <- lat.lng.df %>% leaflet() %>%
   addTiles() %>%
   addMarkers(popup=asulinks) %>%
   addMarkers(lat=33.4218, lng=-111.9399, icon=asuicon) %>%
 addCircles(lng=cities$lng, lat=cities$lat, weight=1, 
            radius=sqrt(cities$popul)*7, fillOpacity=0.04, opacity=0.35) %>%
 addMarkers(lng=cities$lng, lat=cities$lat,
            popup=paste(cities$name, " <br>  pop: ",format(cities$population, 
                        big.mark=",", big.interval=3L) ,sep=""),
            icon=anglerIcon)
mymap